package wiki.zimo.domain;

import java.util.List;
import java.util.Objects;

/**
 * @version v1.0
 * @auther: 子墨
 * @datetime: 2018/11/4 14:51
 * @description: 分页实体类
 */
public class Page<T> {

    private int pageNumber = 1;// 当前的页码，默认1
    private int pageSize = 9;// 每页显示的数量，默认9
    private int totalRecord;// 记录数
    private int totalPage;// 总页数
    private List<T> data;// 数据

    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(int totalRecord) {
        this.totalRecord = totalRecord;
    }

    public int getTotalPage() {
        // 页数等于记录数/每页展示的记录数 + 1
        totalPage = (int) Math.ceil(totalRecord * 1.0 / pageSize);
        return totalPage;
    }

    /**
     * 获取开始索引
     * 每页数据的开始索引等于当前页页码-1然后乘以每一页的商品数量
     *
     * @return
     */
    public int getStartIndex() {
        return (pageNumber - 1) * pageSize;
    }

    public Page(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public Page() {

    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Page<?> page = (Page<?>) o;
        return pageNumber == page.pageNumber &&
                pageSize == page.pageSize &&
                totalRecord == page.totalRecord &&
                totalPage == page.totalPage &&
                Objects.equals(data, page.data);
    }

    @Override
    public int hashCode() {

        return Objects.hash(pageNumber, pageSize, totalRecord, totalPage, data);
    }

    @Override
    public String toString() {
        return "Page{" +
                "pageNumber=" + pageNumber +
                ", pageSize=" + pageSize +
                ", totalRecord=" + totalRecord +
                ", totalPage=" + totalPage +
                ", data=" + data +
                '}';
    }
}
